/** 
 * @Title: AreaCrawl.java 
 * @Package com.zizo.core 
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月22日 下午3:54:26 
 */
package com.zizo.core;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;

/**
 * @ClassName: AreaCrawl
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月22日 下午3:54:26
 * 
 */
public class AreaCrawl {
	/**
	 * @Title: getArea
	 * @Description: TODO 城市和地区对照表
	 * @return
	 * @return Map<String,String>
	 * @author Alexis
	 */
	public static final Map<String, String> getArea() throws Exception {
		Map<String, String> map = new HashMap<>();
		String url = "http://www.autohome.com.cn/AreaList.html";
		Document doc = DocUtil.getDoc(url);
		Elements areas = doc.select("div.cityList div.tborder h3");
		Elements citys = doc.select("div.cityList table.font14WithLine");
		for (int i = 0; i < 7; i++) {
			Element area = areas.get(i);
			Element city = citys.get(i);
			Elements cs = city.select("th");
			for (Element e : cs) {
				// synArea(map, area.text(), e.text());
				String s = e.text();
				if (s.equalsIgnoreCase(" ")) {
					continue;
				}
				synArea(map, area.text(), s.split("：")[0]);
			}
		}
		synArea(map, "西南地区", "重庆");
		synArea(map, "华北地区", "北京");
		synArea(map, "华北地区", "天津");
		synArea(map, "华东地区", "上海");
		return map;
	}

	static Connection conn = ConnectionPool.getConnection("core", "root", "", "localhost");

	public static final void synArea(Map<String, String> map, String w1, String w2) throws Exception {
		String sql = "UPDATE core_area SET area_region=? where area_province=?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, w1);
		ps.setString(2, w2);
		ps.executeUpdate();
	}

	public static void main(String[] args) throws Exception {
		AreaCrawl.getArea();
	}
}
